/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const ArticleHrefAdapter = require('../ArticleHrefAdapter');
const cheerio = require('cheerio');
const urlTool = require('url');

/**
 * 山东地方动态
 * @type {module.Bjp2p}
 */
module.exports = class ShanDongDFDT extends ArticleHrefAdapter {


    parseHtml(html, linkRule) {
        //将解析后的内容加载进解析器
        html = html.replace('<script type="text/xml"><datastore>\n' +
          '<nextgroup><![CDATA[<a href="/module/web/jpage/dataproxy.jsp?page=1&appid=1&appid=1&webid=14&path=/&columnid=6013&unitid=26239&webname=%25E5%25B1%25B1%25E4%25B8%259C%25E7%259C%2581%25E4%25BD%258F%25E6%2588%25BF%25E5%2592%258C%25E5%259F%258E%25E4%25B9%25A1%25E5%25BB%25BA%25E8%25AE%25BE%25E5%258E%2585&permissiontype=0"></a>]]></nextgroup>\n' +
          '<recordset>','')
        html = html.replace(/<record><\!\[CDATA\[/gi,'')
        html = html.replace(/\]\]><\/record>/gi,'')
        html = html.replace('</recordset>\n' +
          '</datastore></script>','')
        let $ = cheerio.load(html);
        let contents = [];
        let that = this;
        let rangeText = '';
        $(this.linkRule).each(function () {
            let title = $(this).text().trim();
            if(!/\([0-9]+\)/g.test(title) && !/（[0-9]+）/g.test(title)) {
                let href = '';
                if ($(this).is('a') && $(this).attr('href')) {
                    href = urlTool.resolve(that.requestUrl, $(this).attr('href'));
                }

                if (title.length != 0 || href.length != 0) {
                    contents.push({
                        title: title,
                        href: href
                    });
                    rangeText += title + href;
                }
            }


        });

        return {
            rangeText: rangeText,
            contents: contents
        };
    }


}
